package com.qingfeng.activiti.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingfeng.activiti.service.INodesettingService;
import com.qingfeng.base.controller.BaseController;
import com.qingfeng.entity.MyResponse;
import com.qingfeng.entity.activiti.Nodesetting;
import com.qingfeng.utils.*;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @ProjectName NodesettingController
 * @author Administrator
 * @version 1.0.0
 * @Description 流程节点设置
 * @createTime 2022/12/5 0005 21:55
 */
@Controller
@RequestMapping(value = "/activiti/nodesetting")
public class NodesettingController extends BaseController {

	@Autowired
	private INodesettingService nodesettingService;
	@Autowired
	private RuntimeService runtimeService;


	/**
	 * @title findActivitiSetting
	 * @description 查询流程节点设置
	 * @author Administrator
	 * @updateTime 2022/12/5 0005 21:57
	 */
	@GetMapping("/settingInfo")
	public void settingInfo(Nodesetting nodesetting, HttpServletResponse response) throws IOException  {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("process_id",nodesetting.getProcess_id());
		queryWrapper.eq("node_id",nodesetting.getNode_id());
		queryWrapper.orderByDesc("create_time");
		Nodesetting data = nodesettingService.getOne(queryWrapper);
		Json json = new Json();
		json.setMsg("获取数据成功。");
		json.setData(data);
		json.setSuccess(true);
		this.writeJson(response,json);
	}


	/**
	 *
	 * @param nodesetting
	 * @param response
	 * @throws IOException
	 */
	@GetMapping("/findSettingInfo")
	public void findSettingInfo(HttpServletRequest request, HttpServletResponse response) throws IOException  {
		PageData pd = new PageData(request);
		System.out.println("--------------------findSettingInfo----------------------");
		ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
		ProcessInstance processInstance = processInstanceQuery.processInstanceId(pd.get("processInstanceId").toString()).singleResult();
		Json json = new Json();
		if(Verify.verifyIsNotNull(processInstance)){
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("process_id",processInstance.getProcessDefinitionKey());
			queryWrapper.eq("node_id",pd.get("node_id"));
			queryWrapper.orderByDesc("create_time");
			Nodesetting data = nodesettingService.getOne(queryWrapper);
			json.setData(data);
		}
		json.setMsg("获取数据成功。");
		json.setSuccess(true);
		this.writeJson(response,json);
	}



	/** 
	 * @Description: save
	 * @Param: [request, response, session] 
	 * @return: void 
	 * @Author: anxingtao
	 * @Date: 2020-11-8 21:29 
	 */
	@PostMapping("/save")
	public void save(@Valid @RequestBody Nodesetting nodesetting , HttpServletResponse response) throws IOException  {
		String time = DateTimeUtil.getDateTimeStr();
		//获取当前登录用户信息
		String user_id = "1";
		String organize_id = "1";
		nodesetting.setType("1");
		if(Verify.verifyIsNotNull(nodesetting.getId())){
			nodesetting.setUpdate_time(time);
			nodesetting.setUpdate_user(user_id);
			nodesettingService.updateById(nodesetting);
		}else{
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("process_id",nodesetting.getProcess_id());
			queryWrapper.eq("node_id",nodesetting.getNode_id());
			queryWrapper.orderByDesc("create_time");
			Nodesetting data = nodesettingService.getOne(queryWrapper);
			if(Verify.verifyIsNull(data)){
				//主键id
				String id = GuidUtil.getUuid();
				nodesetting.setId(id);
				nodesetting.setCreate_time(time);
				nodesetting.setCreate_user(user_id);
				nodesetting.setCreate_organize(organize_id);
				nodesettingService.save(nodesetting);
			}else{
				nodesetting.setUpdate_time(time);
				nodesetting.setUpdate_user(user_id);
				nodesettingService.updateById(nodesetting);
			}
		}
		Json json = new Json();
		json.setSuccess(true);
		json.setData(nodesetting);
		json.setMsg("操作成功。");
		this.writeJson(response,json);
	}

	/**
	 * 根据流程id查询流程下面的所有节点信息
	 * @param nodesetting
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/findList")
	public MyResponse findList(Nodesetting nodesetting) throws IOException  {
		//处理数据权限
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("process_id",nodesetting.getProcess_id());
		List<Nodesetting> list = nodesettingService.list(queryWrapper);
		return new MyResponse().data(list);
	}



}
